// ==============================================================================================
// Ribbon.cs
//
// Created by: NI, 2007.03.26
// Revised by: NI, 2007.11.23
// ==============================================================================================
using System.Collections.Generic;
using System.Linq;
using RibbonExplorer.Entities;
using RibbonExplorer.Storage;

namespace RibbonExplorer.Entities
{
  // ==============================================================================================
  /// <summary>
  /// This class represents a ribbon entity that belongs any of the Office applications.
  /// </summary>
  // ==============================================================================================
  public sealed class Ribbon : ItemBase
  {
    #region Private fields

    private string _Name;
    private OfficeApp _ParentApplication;
    private List<RibbonTab> _Tabs = new List<RibbonTab>();
    private List<TabSet> _TabSets = new List<TabSet>();
    private OfficeMenu _OfficeMenu;
    private QatMenu _Qat;
    private NotInTheRibbon _NotInTheRibbon;

    #endregion

    #region Lifecyle methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Creates a ribbon instance with the specified ID and name.
    /// </summary>
    /// <param name="id">Ribbon ID</param>
    /// <param name="name">Application name</param>
    /// <param name="parent">Parent application</param>
    // --------------------------------------------------------------------------------------------
    public Ribbon(string id, string name, OfficeApp parent)
      : base(id)
    {
      _Name = name;
      _ParentApplication = parent;
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the name of the ribbon.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public string Name
    {
      get { return _Name; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent application belonging to this ribbon
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public OfficeApp ParentApplication
    {
      get { return _ParentApplication; }
    } 
    
    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the tabs belonging to this ribbon
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public List<RibbonTab> Tabs
    {
      get { return _Tabs; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the tabsets belonging to this ribbon.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public List<TabSet> TabSets
    {
      get { return _TabSets; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the Office menu of this ribbon.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public OfficeMenu OfficeMenu
    {
      get { return _OfficeMenu; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the Quick Access Toolbar of this ribbon.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public QatMenu Qat
    {
      get { return _Qat; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the controls that do not belong to the ribbon.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public NotInTheRibbon NotInTheRibbon
    {
      get { return _NotInTheRibbon; }
    }

    #endregion

    #region Overridden methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Loads the tabs and tabsets belonging to this ribbon
    /// </summary>
    // --------------------------------------------------------------------------------------------
    protected override void OnLoadChildren()
    {
      // --- Try to obtain the ribbon table
      RibbonTable ribbonTable;
      if (RibbonTableCache.TryGetValue(Id, out ribbonTable))
      {
        var tabs = from control in ribbonTable.ControlList
                   where control.Type == ControlType.Tab &&
                         control.TabSet == "None (Core Tab)"
                   orderby control.Ordering
                   select control;

        foreach (RibbonTableItem item in tabs)
        {
          RibbonTab newTab = new RibbonTab(item.ControlId, this);
          _Tabs.Add(newTab);
        }

        var tabSets = from control in ribbonTable.ControlList
                      where control.Type == ControlType.TabSet
                      orderby control.Ordering
                      select control;

        foreach (RibbonTableItem item in tabSets)
        {
          TabSet newTabSet = new TabSet(item.ControlId, this);
          _TabSets.Add(newTabSet);
        }
      }
      _OfficeMenu = new OfficeMenu(this);
      _Qat = new QatMenu(this);
      _NotInTheRibbon = new NotInTheRibbon(this);
    }

    #endregion
  }
}